<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="isShow=true"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="roleList">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">

                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="compileFn(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delFn(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :page-size="pag.pagesize"
                layout="prev, pager, next"
                :total="total"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
        <!-- 弹窗组件 -->
        <el-dialog :title="formData.id?'编辑角色':'新增角色'" :visible="isShow" @close="cancelFn">
          <el-form ref="addForm" :model="formData" :rules="rules">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="formData.name" />
            </el-form-item>
            <el-form-item label="角色描述" prop="description">
              <el-input v-model="formData.description" />
            </el-form-item>
          </el-form>
          <!-- 足部模块 -->
          <template #footer>
            <el-button type="primary" @click="postRole">确定</el-button>
            <el-button @click="cancelFn">取消</el-button>

          </template>
        </el-dialog>
      <!-- 弹窗组件 -->
      </el-card>
    </div>
  </div>
</template>

<script>
import { getCompany, getEmployeeList, delEmployee, postEmployee, putEmployee } from '@/api/setting'
export default {
  data() {
    return {
      // 弹窗的input 内角色数据
      formData: {
        name: '',
        description: ''
      },
      // 校验规则
      rules: {
        name: [{ required: true, message: '数据不能为空', tigger: 'blur' }],
        description: [{ required: true, message: '数据不能为空', tigger: 'blur' }]
      },
      // 控制弹窗的变量
      isShow: false,
      companyData: {},
      // 页面分页对象
      pag: {
        page: 1,
        pagesize: 3
      },
      // 列表数据
      roleList: [],
      // 总信息条数
      total: 0

    }
  },
  created() {
    this.getCompanyFn()
    this.getEmployeeFn()
  },
  methods: {
    // 点击编辑按钮触发的函数
    async compileFn(formData) {
      // 显示弹窗
      this.isShow = true
      // 发送请求
      console.log(formData)
      // 数据回显
      this.formData = formData
    },
    // 点击取消按钮触发函数
    cancelFn() {
      // 清空数据
      this.$refs.addForm.resetFields()
      // 关闭弹窗
      this.isShow = false
      // 清理数据
      this.formData = {
        name: '',
        description: ''
      }
    },
    // 确认按钮添加角色的函数
    async postRole() {
      // 数据校验
      await this.$refs.addForm.validate()
      // 发送请求
      // 判断是修改还是新增
      if (this.formData.id) {
        // 修改
        await putEmployee(this.formData)
      } else {
        // 新增
        await postEmployee(this.formData)
      }
      // 提醒
      this.$message.success('添加成功')
      // 关闭弹窗
      this.isShow = false
      // 重新渲染
      this.getEmployeeFn()
    },
    // 获取公司信息
    async getCompanyFn() {
      const company_data = await getCompany(1)
      this.companyData = company_data
    },
    // 获取角色信息
    async getEmployeeFn() {
      const { rows, total } = await getEmployeeList(this.pag)
      this.roleList = rows
      this.total = total
    },
    // 点击切换页面触发的函数
    handleCurrentChange(newpage) {
      this.pag.page = newpage
      // 重新渲染页面
      this.getEmployeeFn()
    },
    // 删除角色触发函数
    async delFn(id) {
      // 再次确认
      await this.$confirm('您确认要删除么?')
      // 发送请求
      const res = await delEmployee(id)
      console.log(res)
      // 重新渲染页面
      // 提示删除成功
      this.$message.success('删除成功')
      this.getCompanyFn()
    }

  }

}
</script>

<style>

</style>
